// $Id: mprintf.c 206 2013-07-22 18:16:04Z alan.watson.f $

#include <stdarg.h>
#include <stdlib.h>
#include <stdio.h>

#include "oaxaca-config.h"
#include "oaxaca.h"
#include "mprintf.h"

// The mprintf and vmprintf functions are portable versions of the GNU C
// library asprintf and vasprintf functions. They are version of the
// sprintf family that create a formatted string in allocated memory.

char *
vmprintf(const char *format, va_list ap)
{
  va_list aq;
  va_copy(aq, ap);
  int n = vsnprintf(NULL, 0, format, ap);
  char *s = malloc(n + 1);
  if (s == NULL)
    oaxaca_error("malloc failed in vmprintf.");
  vsprintf(s, format, aq);
  return s;  
}

char *
mprintf(const char *format, ...)
{
  va_list ap;
  va_start(ap, format);
  char *s = vmprintf(format, ap);
  va_end(ap);
  return s;  
}
